﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using JCTimes.DomainModel.Views;
using JCTimes.DomainModel.Entity;
using JCTimes.EFRepository.Specification;
using JCTimes.Portal.Infrastructure;
using System.Data.SqlClient;
using JCTimes.Portal.Models;

namespace JCTimes.Portal.Specification
{
    public class UserSpecification
    {
        public static Expression<Func<User, bool>> Get_User_Pred(Searcher searcher)
        {
            Specification<User> spec = new TrueSpecification<User>();
            spec &= new DirectSpecification<User>(x => x.Flag == 0);
            foreach (var key in searcher.Keys)
            {
                switch (key)
                {
                    case "username":
                        string username = searcher[key];
                        spec &= new DirectSpecification<User>(x => x.UserName != null && x.UserName.Contains(username));
                        break;
                    case "name":
                        string name = searcher[key];
                        spec &= new DirectSpecification<User>(x => x.Name != null && x.Name.Contains(name));
                        break;
                    case "contactname":
                        string email = searcher[key];
                        spec &= new DirectSpecification<User>(x => x.Email != null && x.Email.Contains(email));
                        break;
                    default:
                        break;
                }
            }

            return spec.SatisfiedBy();
        }


        public static Expression<Func<V_Users, bool>> Get_V_User_Pred(Searcher searcher)
        {
            UserInfo uinfo = SessionHelper.CurrUser;

            Specification<V_Users> spec = new TrueSpecification<V_Users>();
            if (uinfo.RoleID == Constant.Admin)
            {
                spec &= new DirectSpecification<V_Users>(x => x.Flag == 0);
            }
            else if (uinfo.RoleID == Constant.Agency)
            {
                spec &= new DirectSpecification<V_Users>(x => x.Flag == 0 && x.ParentID==uinfo.UserID);
            }
            
            foreach (var key in searcher.Keys)
            {
                switch (key)
                {
                    case "username":
                        string username = searcher[key];
                        spec &= new DirectSpecification<V_Users>(x => x.UserName != null && x.UserName.Contains(username));
                        break;
                    case "name":
                        string name = searcher[key];
                        spec &= new DirectSpecification<V_Users>(x => x.Name != null && x.Name.Contains(name));
                        break;
                    case "parentid":
                        string parentid = searcher[key];
                        spec &= new DirectSpecification<V_Users>(x => x.ParentID==parentid);
                        break;
                    default:
                        break;
                }
            }

            return spec.SatisfiedBy();
        }
    }
}